package com.pay.modules.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pay.common.util.DateUtils;
import com.pay.common.util.JwtUtil;
import com.pay.payment.entity.Pay;
import org.apache.commons.codec.digest.DigestUtils;
import com.pay.payment.entity.User;
import com.pay.payment.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.awt.SystemColor.info;

@Api(tags ="支付后台")
@Controller
@RequestMapping(value = "pay")
public class PayController {

	@Autowired
	IUserService userService;

	private static final Logger logger = LoggerFactory.getLogger(PayController.class);

	@ApiOperation(value="登陆")
	@RequestMapping(value="login",method=RequestMethod.POST)
	public @ResponseBody String login(String account,String password) {
		logger.info("==========[userLogin]======mobile ={}",account);
		String param = "false";
		try {
			List<Pay> payList = new ArrayList<>();
			QueryWrapper<User> queryWrapper = new QueryWrapper();
			queryWrapper.eq("mobile", account);
			User user = userService.getOne(queryWrapper);
			if(user != null){
				if(user.getStatus()==0){
					logger.warn("status is error");
					return param;
				}else {
					//21218cca77804d2ba1922c33e0151105
					String md5Result =	DigestUtils.md5Hex(password);
					logger.info("User is md5Result:{}",md5Result);
					if(!md5Result.equals(user.getPasswd())){
						logger.warn("md5Hex is error");
						return param;
					}else {
                        //生成JWT字符串
                        Map<String, Object> info = new HashMap<>();
                        info.put("username", user.getName());
                        info.put("pass", password);
                        JwtUtil.sign(user.getId().toString(), info);
                        param="true";
					}
				}
			}else {
				logger.warn("User is null");
				return param;
			}
		} catch (Exception e) {
			logger.error("==========[login. Exception]->:{}", e);
		}
		return param;
	}
	@ApiOperation(value="后台展示")
	@RequestMapping(value="main",method=RequestMethod.GET)
	public String main(Model model) {
		model.addAttribute("ip", "127.0.0.1");
		model.addAttribute("address", "上海");
		model.addAttribute("time", DateUtils.getTime());
		return "web/main";
	}
}
